using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MyHelper;
using clf.weighing.entity;

namespace weighing
{

    public class CameraInfoVo : BaseVo
    {
        #region Constract
        public CameraInfo mCameraInfo;
        public CameraInfoVo(CameraInfo cameraInfo)
        {
            mCameraInfo = cameraInfo;
        }
        #endregion


        /// <summary>
        /// 可空:NO
        /// </summary>

        public Int64 Id { get { return mCameraInfo.id; } set { mCameraInfo.id = value; OnPropertyChanged(this, nameof(Id)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public String Ip { get { return mCameraInfo.ip; } set { mCameraInfo.ip = value; OnPropertyChanged(this, nameof(Ip)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public Int32? Port { get { return mCameraInfo.port; } set { mCameraInfo.port = value; OnPropertyChanged(this, nameof(Port)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public String UserName { get { return mCameraInfo.userName; } set { mCameraInfo.userName = value; OnPropertyChanged(this, nameof(UserName)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public String Password { get { return mCameraInfo.password; } set { mCameraInfo.password = value; OnPropertyChanged(this, nameof(Password)); } }

        /// <summary>
        /// 注释:位置(1 前 2 后 3 上 4屏幕截图)
        /// 可空:YES
        ///默认值:0
        /// </summary>

        public Int32? Position { get { return mCameraInfo.position; } set { mCameraInfo.position = value; OnPropertyChanged(this, nameof(Position)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public Int64? ScaleId { get { return mCameraInfo.scaleId; } set { mCameraInfo.scaleId = value; OnPropertyChanged(this, nameof(ScaleId)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public Int64? ClientId { get { return mCameraInfo.clientId; } set { mCameraInfo.clientId = value; OnPropertyChanged(this, nameof(ClientId)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public Int64? CompanyId { get { return mCameraInfo.companyId; } set { mCameraInfo.companyId = value; OnPropertyChanged(this, nameof(CompanyId)); } }

        /// <summary>
        /// 注释:状态 1正常 0禁用
        /// 可空:NO
        /// 默认值:1
        /// </summary>

        public Int32 Status { get { return mCameraInfo.status; } set { mCameraInfo.status = value; OnPropertyChanged(this, nameof(Status)); } }

        /// <summary>
        /// 可空:YES
        /// 默认值:0
        /// </summary>

        public Int32? IsDelete { get { return mCameraInfo.isDelete; } set { mCameraInfo.isDelete = value; OnPropertyChanged(this, nameof(IsDelete)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public DateTime? DeleteTime { get { return mCameraInfo.deleteTime; } set { mCameraInfo.deleteTime = value; OnPropertyChanged(this, nameof(DeleteTime)); } }

        /// <summary>
        /// 可空:YES
        /// </summary>

        public Int64? SyncTime { get { return mCameraInfo.syncTime; } set { mCameraInfo.syncTime = value; OnPropertyChanged(this, nameof(SyncTime)); } }

        /// <summary>
        /// 注释:是否截图
        ///默认值:0
        /// </summary>

        public Int32? IsCapture
        {
            get { return mCameraInfo.isCapture; }
            set
            {
                mCameraInfo.isCapture = value;
                OnPropertyChanged(this, nameof(IsCapture));
                OnPropertyChanged(this, nameof(CaptureText));
            }
        }

        /// <summary>
        /// 注释:是否为车辆识别的摄像头
        /// 可空:YES
        ///默认值:0
        /// </summary>

        public Int32? IsRecognize
        {
            get { return mCameraInfo.isRecognize; }
            set
            {
                mCameraInfo.isRecognize = value;
                OnPropertyChanged(this, nameof(IsRecognize));
                OnPropertyChanged(this, nameof(RecognizeText));
            }
        }

        /// <summary>
        /// 注释:摄像头位置名称
        /// 可空:YES
        /// </summary>

        public String PositionName { get { return mCameraInfo.positionName; } set { mCameraInfo.positionName = value; OnPropertyChanged(this, nameof(PositionName)); } }

        /// <summary>
        /// 注释: 品牌类型 1  海康 2 火眼臻识
        /// 可空:YES
        ///默认值:-1
        /// </summary>

        public Int32? BrandType { get { return mCameraInfo.brandType; } set { mCameraInfo.brandType = value; OnPropertyChanged(this, nameof(BrandType)); } }

        /// <summary>
        /// 注释:是否播放视频 0 否 1 是
        /// 可空:NO
        ///默认值:0
        /// </summary>

        public Int32 IsPlay { get { return mCameraInfo.isPlay; } set { mCameraInfo.isPlay = value; OnPropertyChanged(this, nameof(IsPlay)); } }

        /// <summary>
        /// 注释:识别类型 0 未知 1 入场 2出场
        /// 可空:NO
        ///默认值:1
        /// </summary>

        public Int32 DirectionType { get { return mCameraInfo.directionType; } set { mCameraInfo.directionType = value; OnPropertyChanged(this, nameof(DirectionType)); } }


        #region
        private ICamera sdk;

        /// <summary>
        /// 操作摄像头的SDK 助手
        /// </summary>
        public ICamera SdkHepler
        {
            get
            {
                if (sdk == null)
                {
                    if (BrandType == (int)CameraBrand.HYZS)
                    {
                        sdk = new HYZSCameraHelper();
                    }
                    else
                    {
                        sdk = new HKCameraHelper();
                    }
                }
                return sdk;
            }
            set { sdk = value; }
        }

        public int LoginHandle { get; set; } = -1;
        /// <summary>
        /// 通道号
        /// </summary>
        public int ChanNum { get; set; }
        public int PreviewHandle { get; set; } = -1;
        /// <summary>
        /// 预览 PictureBox 句丙
        /// </summary>
       // public PictureBox Picture { get; set; }

        public IntPtr PictureHandle { get; set; }

        public int Login()
        {
            CHCNetSDK.NET_DVR_DEVICEINFO_V40 devieInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();
            if (BrandType == (int)CameraBrand.HK)
            {
                this.LoginHandle = SdkHepler.Login(Ip, (int)Port, UserName, Password, ref devieInfo);
                if (LoginHandle < 0)
                {
                    var res = CHCNetSDK.NET_DVR_GetLastError();
                    var msg = "login failure NET_DVR_GetLastError(), code:" + res;
                    Log.Error(msg);
                }
                this.ChanNum = devieInfo.struDeviceV30.byChanNum;
            }
            else
            {
                this.LoginHandle = SdkHepler.Login(Ip, (int)Port, UserName, Password);
            }

            return LoginHandle;
        }

        public int PreView(out uint errCode)
        {
            PreviewHandle = SdkHepler.Preview(PictureHandle, LoginHandle, ChanNum, out errCode);
            return PreviewHandle;
        }

        public bool CaptureJpeg(string file, int quality = 80)
        {
            //没有预览成功
            if (PreviewHandle == -1) return false;
            bool res = SdkHepler.CaptureJpeg(PreviewHandle, file, ChanNum, quality);
            if (!res && BrandType == (int)CameraBrand.HK)
            {
                res = HKCameraHelper.CaptureBmp(PreviewHandle, file);
            }
            return res;
        }
        #endregion

        #region 文字

        public string RecognizeText { get { return mCameraInfo.isRecognize == 1 ? "是" : "否"; } }
        public string BrandTypeText
        {
            get
            {
                if (BrandType == null) return "未知品牌";

                CameraBrand brand = (CameraBrand)BrandType;
                string res;
                switch (brand)
                {
                    case CameraBrand.HK:
                        res = "海康微视";
                        break;
                    case CameraBrand.HYZS:
                        res = "火眼臻识";
                        break;
                    default:
                        res = "未知品牌";
                        break;
                }
                return res;
            }
        }

        public string DirectionText
        {
            get
            {
                switch (mCameraInfo.directionType)
                {
                    case 0:
                        return "未知";
                    case 1:
                        return "入口";
                    case 2:
                        return "出口";
                    default:
                        return "未知";
                }
            }
        }

        public string CaptureText { get { return mCameraInfo.isCapture == 1 ? "是" : "否"; } }
        #endregion
    }
}
